package net.oschina.simpleplus;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import net.oschina.simpleplus.client.Packet;
import net.oschina.simpleplus.client.PacketClient;
import net.oschina.simpleplus.client.ResponseFuture;
import net.oschina.simpleplus.core.InvokeCallback;

public class Startup {
	private static final int NUM = 100;
	
	public static void main(String[] args) throws Exception {
		PacketClient client = new PacketClient();
        client.init();
        client.start();

        // 测试同步请求，关注rqid是否对应
        for (int i = 0; i < NUM; i++) {
            Packet sdkProto = new Packet();
            System.out.println(i+" sendProto: " + sdkProto.toString());
            Packet resultProto = client.invokeSync(sdkProto, 2000);
            System.out.println(i+" resultProto: " + resultProto.toString());
        }
        System.out.println(">>>>>>invokeync test finish");

        // 测试异步请求，关注rqid是否对应
        final CountDownLatch countDownLatch = new CountDownLatch(NUM);
        for (int i = 0; i < NUM; i++) {
            Packet sdkProto = new Packet();
            int finalI = i;
            client.invokeAsync(sdkProto, 20000, responseFuture -> {
                    System.out.println(finalI + " sendProto: " + sdkProto.toString());
                    countDownLatch.countDown();
                    System.out.println(finalI + " resultProto: " + responseFuture.getPacket().toString());
            });
        }
        countDownLatch.await(10, TimeUnit.SECONDS);
        System.out.println(">>>>>>invokeAsync test finish");
	}
}
